<template>
  <div>
    <el-button type="success" size='small' @click="routeConfig(1)" class="bgpReleaseClick">{{$t('发布路由')}}</el-button>
    <el-button type="danger" size='small' @click="routeConfig(2)" class="bgpShowClick">{{$t('展示路由')}}</el-button>
    <div v-if="showConfig === 1" class="bgpConfigTable">
      <el-button type="success" size='small' @click="dialogVisible()" class="bgpAddClick">{{$t('添加')}}</el-button>
      <el-button type="danger" size='small' @click="deleteData()" class="bgpDelClick">{{$t('删除')}}</el-button>
      <el-button type="success" size='small' @click="reloadRip()" icon="el-icon-refresh" class="bgpReClick">{{$t('重载配置')}}</el-button>
      <el-table size='small' ref="multipleConfigTable" :data="bgpList" tooltip-effect="dark" @selection-change="handleSelectionChange">
        <el-table-column type="selection" align='center' width="55"></el-table-column>
        <el-table-column prop="asn" :label="$t('本地AS号')" align='center' min-width="100"></el-table-column>
        <el-table-column prop="vrf" :label="$t('VRF名称')" align='center' min-width="100"></el-table-column>
        <el-table-column prop="router_id" :label="$t('路由器ID')" align='center' min-width="100"></el-table-column>
        <el-table-column prop="publish_route" :label="$t('发布路由')" align='center' min-width="100">
          <template slot-scope="scope">
            <tooltip :html="publishRouteShowTooltip(scope.row,';<br/>')" :content="publishRouteShowTooltip(scope.row,',')" class="wid190" :refName="scope.row.id"></tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="aggregated_route" :label="$t('路由聚合')" align='center' min-width="100">
          <template slot-scope="scope">
            <tooltip :html="aggregatedRouteTooltip(scope.row,';<br/>')" :content="aggregatedRouteTooltip(scope.row,',')" class="wid190" :refName="scope.row.id"></tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="peer" :label="$t('对等体')" align='center' min-width="100">
          <template slot-scope="scope">
            <tooltip :html="peerShowTooltip(scope.row,';<br/>')" :content="peerShowTooltip(scope.row,',')" class="wid190" :refName="scope.row.id"></tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="right" align='center' fixed="right" :label="$t('操作')" width="150">
          <template slot-scope="scope">
            <el-button type="success" plain size="mini" @click="dialogVisible(scope.row, '编辑')" :class="'bgpEdit' +(scope.$index+1)">{{$t('编辑')}}</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-table size='small' ref="multipleTable" :data="tableList" tooltip-effect="dark" style="width: 100%;margin-top:20px;" v-if="showConfig === 2">
      <el-table-column prop="vrf" :label="$t('VRF名称')" align='center' min-width="200"></el-table-column>
      <el-table-column prop="destination" :label="$t('目的地址')" align='center' min-width="200"></el-table-column>
      <el-table-column prop="next_hop" :label="$t('下一跳')" align='center' min-width="200"></el-table-column>
      <el-table-column prop="out_interface" :label="$t('出接口')" align='center' min-width="200"></el-table-column>
    </el-table>
    <bgp-dialog v-if="bgpDialogVisible" @listData='listData' ref="refBgpDialogVisible"></bgp-dialog>
  </div>
</template>

<script>
import BgpDialog from './BgpDialog'
import {
  getBgpRoute, // BGP获取展示路由
  getBgpInfo, // BGP获取协议信息
  delBgp // Bgp删除路由
} from '@/api/network/route/dynamicRoute/BGP'
import { reloadAgreement } from '@/api/network/route/dynamicRoute/RIP'
export default {
  name: 'BGP',
  components: {
    BgpDialog
  },
  data () {
    return {
      bgpDialogVisible: false,
      record: {
        status: 'off',
        net: '',
        is_type: '',
        lsp_gen: '',
        lsp_refresh: '',
        max_lsp_lifetime: '',
        spf_interval: ''
      },
      name: '',
      old_net: '',
      showConfig: 1,
      tableList: [],
      bgpList: [],
      modeList: {
        'md5': 'MD5',
        'text': this.$t('明文'),
        'off': this.$t('关闭认证')
      },
      netReversal: {
        'off': 'on',
        'on': 'off'
      }
    }
  },
  methods: {
    listData () {
      this.showConfig = 1
      getBgpInfo().then(res => {
        if (res.status === 0) {
          this.bgpList = res.info
        }
      })
    },
    dialogVisible (data, type) {
      this.bgpDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refBgpDialogVisible.init(data, type)
      })
    },
    // 多选按钮
    handleSelectionChange (data) {
      var name = ''
      for (var item of data) {
        if (name === '') {
          name = item.id
        } else {
          name = name + '@#@' + item.id
        }
      }
      this.name = name
    },
    routeConfig (data) {
      this.showConfig = data
      if (data === 2) {
        getBgpRoute().then(res => {
          if (res.status === 0) {
            this.tableList = res.info
          }
        })
      }
    },
    deleteData () {
      if (this.name) {
        this.$confirm(this.$t('您是否要删除所选中的数据？'), this.$t('提示'), {
          closeOnClickModal: false,
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          confirmButtonClass: 'confirmButton',
          cancelButtonClass: 'confirmCancelButton',
          type: 'warning'
        }).then(() => {
          delBgp({ id: this.name }).then(res => {
            if (res.status === 0) {
              this.$messages.success(this.$t('删除成功'))
              getBgpInfo().then(res => {
                if (res.status === 0) {
                  this.bgpList = res.info
                }
              })
            }
          })
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据进行删除'))
      }
    },
    reloadRip () {
      reloadAgreement({ agreement: 'bgp' }).then(res => {
        if (res.status === 0) {
          this.listData()
          this.$messages.success(this.$t('重载成功'))
        }
      })
    },
    peerShowTooltip (data, str) {
      let reStr = ''
      if (data.neighbor[0]) {
        data.neighbor[0].forEach((val, index) => {
          if (index < 1 && str === ',') {
            reStr += val.peer
          } else if (str === ',') {
            reStr += str + val.peer
          } else {
            reStr += val.peer + str
          }
        })
      }
      return reStr
    },
    publishRouteShowTooltip (data, str) {
      let reStr = ''
      if (data.network[0]) {
        data.network[0].forEach((val, index) => {
          if (index < 1 && str === ',') {
            reStr += val.ip + '/' + val.mask
          } else if (str === ',') {
            reStr += str + val.ip + '/' + val.mask
          } else {
            reStr += val.ip + '/' + val.mask + str
          }
        })
      }
      return reStr
    },
    aggregatedRouteTooltip (data, str) {
      let reStr = ''
      if (data.ggregate_address[0]) {
        data.ggregate_address[0].forEach((val, index) => {
          if (index < 1 && str === ',') {
            reStr += val.ip + '/' + val.mask
          } else if (str === ',') {
            reStr += str + val.ip + '/' + val.mask
          } else {
            reStr += val.ip + '/' + val.mask + str
          }
        })
      }
      return reStr
    }
  }
}
</script>

<style lang="less" scoped>
.ripDescriptions {
  .el-switch {
    margin-left: 15px;
    /deep/ .el-switch__core {
      &::before {
        content: "关闭";
        color: #fff;
        position: absolute;
        // font-size: 14px;
        transform: scale(0.85);
        left: 20px;
      }
    }
    &.switch-is-actived {
      /deep/ .el-switch__core {
        &::before {
          content: "开启";
          left: 8px;
          transform: scale(0.85);
        }
      }
    }
  }
}
.selectBox {
  padding: 0 20px;
}
.bgpConfigTable {
  margin-top: 10px;
  border: 1px solid #e6ebf5;
  padding: 20px;
}
</style>
